﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RioTinto.Web
{
    [ValidationProperty("Text")]
    public partial class GALAutoCompleteBox : System.Web.UI.UserControl
    {
        private String m_NetworkId = String.Empty;

        public GALAutoCompleteBox()
        {

        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public String Text
        {
            get
            {
                return m_UserName.Text;
            }
            set
            {
                m_UserName.Text = value;
                if (String.IsNullOrEmpty(value))
                    m_NetworkId = String.Empty;
            }
        }

        public String NetworkId
        {
            get
            {
                if (String.IsNullOrEmpty(m_NetworkId) && !String.IsNullOrEmpty(m_UserName.Text))
                {
                    GALPerson person = GALDirectory.GetPersonByCN(m_UserName.Text);
                    if (person == null)
                    {
                        throw new ArgumentException(String.Format("The name '{0}' could not be found in the directory.", m_UserName.Text));
                    }

                    m_NetworkId = person.NetworkId;
                }

                return m_NetworkId;
            }
            set
            {
                // reset text
                GALPerson person = GALDirectory.GetPersonByNetworkId(value);
                if (person == null)
                {
                    throw new ArgumentException(String.Format("The NetworkId '{0}' could not be found in the directory.", value));
                }

                m_NetworkId = value;
                m_UserName.Text = person.CName;
            }
        }

        public String AutoCompleteCssClass
        {
            get
            {
                return m_AutoCompleteBlock.Attributes["class"];
            }
            set
            {
                m_AutoCompleteBlock.Attributes.Add("class", value);
            }
        }

        public String TextBoxCssClass
        {
            get
            {
                return m_UserName.CssClass;
            }
            set
            {
                m_UserName.CssClass = value;
            }
        }
    }
}